package com.system.service.impl;

import com.system.mapper.InvoiceMapper;
import com.system.mapper.LogisticsMapper;
import com.system.pojo.Invoice;
import com.system.pojo.Logistics;
import com.system.service.LogisticsService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yjymm
 * @date 2019-12-23 11:17
 */
@Service
public class LogisticsServiceImpl implements LogisticsService {

    @Autowired
    private LogisticsMapper lm;

    @Autowired
    private InvoiceMapper invoiceMapper;


    @Override
    public ResultVo<Logistics> insert(Logistics logistics) {
        ResultVo<Logistics> resultVo = new ResultVo<Logistics>();
        Integer invoiceId = logistics.getInvoiceId();
        Invoice invoice = invoiceMapper.selectByPrimaryKey(invoiceId);
        Logistics logistics1 = lm.selectByInvoiceId(invoiceId);
        if (logistics1 == null){
            //不存在，插入
            lm.insertSelective(logistics);
        }else{
            lm.updateLogisticsNumberByInvoiceId(logistics);
        }
        //发货单更新物流号
        invoice.setLogisticsNumber(logistics.getLogisticsNumber());
        invoiceMapper.updateByPrimaryKeySelective(invoice);
        resultVo.setInsert(true);
        resultVo.setMessage("修改成功");
        return resultVo;
    }
}
